<template>
  <div class="dictionary-manage-container">
    <!-- 顶部筛选条件 -->
    <div class="conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <!-- @change="conditionChange($event, 'status')" -->
          <!-- 字典名称 -->
          <a-input
            v-model.trim="conditions.dictTypeDesc"
            placeholder="请输入字典名称"
          />
        </template>
        <template slot="operate">
          <a-button type="primary" @click="getSearch"> 查询 </a-button>
        </template>
      </ScreenConditions>
    </div>

    <!-- 主体区域 -->
    <div class="result-container">
      <div class="common-result-container-header" header="字典管理 ">
        <a-button type="primary" @click="gotoAdd" v-if="showButton">
          新增
        </a-button>
      </div>
      <div class="result-body">
        <!-- table -->
        <div class="table-container" v-loading="table.loading">
          <vxe-table
            align="center"
            :data="table.source"
            show-overflow
            show-header-overflow
            show-footer-overflow
            show-footer
            :column-config="{ resizable: true }"
            class="scrollbarStyle"
            height="99%"
            :sort-config="{ trigger: 'cell' }"
            ref="xTable"
            :scroll-x="{ gt: 10 }"
            :scroll-y="{ gt: 100 }"
            row-id="id"
            :checkbox-config="{
              reserve: true,
            }"
          >
            <vxe-column
              :title="item.title"
              :field="item.key"
              :key="index"
              :min-width="item.width"
              show-overflow
              v-for="(item, index) in table.columns"
            >
              <template slot-scope="scope">
                <div v-if="item.key == 'status'">
                  <span class="status-text">正常</span>
                </div>

                <div v-else>{{ scope.row[item.key] }}</div>
              </template>
            </vxe-column>
            <vxe-column title="操作" width="280" fixed="right">
              <template #default="{ row }">
                <div class="text-button-group">
                  <a-button
                    type="link"
                    @click="gotoEdit(row)"
                    v-if="showButton"
                  >
                    编辑
                  </a-button>
                  <a-button
                    type="link"
                    @click="gotoDicData(row)"
                    v-if="showButton"
                  >
                    编辑字典数据
                  </a-button>
                  <a-button
                    type="link"
                    class="danger"
                    @click="handleDelClick(row)"
                    v-if="showButton"
                  >
                    删除
                  </a-button>
                </div>
              </template>
            </vxe-column>
          </vxe-table>
        </div>
        <!-- 分页 -->
        <div class="pager-container">
          <vxe-pager
            background
            :current-page.sync="pager.page"
            :page-size.sync="pager.pageSize"
            :total="pager.total"
            :layouts="[
              'PrevPage',
              'JumpNumber',
              'NextPage',
              'Sizes',
              'FullJump',
            ]"
            @page-change="paginationChange"
          >
            <template #left>
              <div class="vxe-pager-left-tip-container">
                第<span>1</span>到<span
                  >{{ Math.ceil(pager.total / pager.pageSize) || 1 }}页 </span
                >共<span>{{ pager.total }}</span
                >条记录
              </div>
            </template>
          </vxe-pager>
        </div>
      </div>
    </div>
    <!-- 新增弹窗 -->
    <addModal :visible.sync="addVisible" :model="model"></addModal>
  </div>
</template>

<script>
//【字典管理】列表
import bjSelect from "@/components/bj-select";
import addModal from "./components/addModal";
import { notificationTip } from "@/utils";
import {
  getDictTypeManageList,
  getDictTypeDelete,
} from "@/api/user-center/dictionaryManage.js";
import { getPurView } from "@/api/user-center/userManage.js";
export default {
  components: {
    bjSelect,
    addModal,
  },
  data() {
    return {
      //顶部筛选条件
      conditions: {
        //字典名称
        dictTypeDesc: "",
      },
      //table
      table: {
        loading: false,
        source: [],
        columns: [
          {
            title: "字典名称",
            dataIndex: "dictTypeDesc",
            key: "dictTypeDesc",
            width: "",
          },
          {
            title: "字典类型",
            dataIndex: "dictTypeKey",
            key: "dictTypeKey",
            width: "",
          },
          {
            title: "排序",
            dataIndex: "sort",
            key: "sort",
            width: "",
          },
          {
            title: "备注",
            dataIndex: "remark",
            key: "remark",
            width: "",
          },
          {
            title: "创建时间",
            dataIndex: "createTime",
            key: "createTime",
            width: "",
          },
        ],
      },
      //分页
      pager: {
        page: 1,
        pageSize: 10,
        total: 0,
      },
      //新增弹窗
      addVisible: false,
      model: {
        id: "",
        text: "",
      },
      //按钮权限
      showButton: false,
    };
  },
  mounted() {
    this.getPurviewFlag();
    this.getTableData();
  },
  methods: {
    //查询操作权限
    getPurviewFlag() {
      getPurView()
        .then((res) => {
          this.showButton = res?.data?.result || false;
        })
        .catch((_) => {
          this.showButton = false;
        });
    },
    //编辑字典数据
    gotoDicData(row) {
      let { dictTypeKey, dictTypeDesc } = row;
      this.$router.push({
        path: `/nebula/user-app/dictionaryManage/dicData`,
        query: {
          dictTypeKey,
          dictTypeDesc,
          showButton: this.showButton,
        },
      });
    },
    //查询列表
    getTableData() {
      this.table.loading = true;
      let { page, pageSize } = this.pager;
      let { dictTypeDesc } = this.conditions;
      return new Promise((resolve, reject) => {
        getDictTypeManageList({
          page,
          pageSize,
          dictTypeDesc,
        })
          .then((res) => {
            let {
              data: { result },
            } = res;
            this.table.source = result?.rows || [];
            this.pager.total = result?.total || 0;
            resolve();
          })
          .catch((_) => {
            this.table.source = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.table.loading = false;
          });
      });
    },
    //查询
    getSearch() {
      this.pager.page = 1;
      this.getTableData();
    },
    //新增
    gotoAdd() {
      this.model.text = "新增";
      this.model.id = "";
      this.addVisible = true;
    },
    //编辑
    gotoEdit(row) {
      this.addVisible = true;
      this.model = {
        text: "编辑",
        id: row.id,
        //字典名称
        dictTypeDesc: row.dictTypeDesc,
        //字典类型
        dictTypeKey: row.dictTypeKey,
        //排序
        sort: row.sort,
        //备注
        remark: row.remark,
      };
    },
    //删除
    handleDelClick(row) {
      let that = this;
      let dataLength = that.table.source.length;
      let { id } = row;
      const h = this.$createElement;
      this.$confirm({
        title: "您确定删除所选内容?",
        content: "删除内容后,将无法恢复。",
        okText: "确定",
        okType: "primary",
        cancelText: "取消",
        centered: true,
        icon: () =>
          h("a-icon", {
            props: {
              type: "question-circle",
              theme: "filled",
            },
            style: "color:#FB5A5A",
          }),
        onOk() {
          getDictTypeDelete(id)
            .then((res) => {
              if (dataLength == 1 && that.pager.page > 1) {
                that.pager.page -= 1;
              }
              notificationTip("success", "删除字典类别成功！");
              that.getTableData();
            })
            .catch();
        },
        onCancel() {},
      });
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getTableData();
    },
  },
};
</script>

<style lang="less" scoped>
.dictionary-manage-container {
  height: 100%;
  .flex();
  flex-direction: column;
  box-sizing: border-box;
  padding: 0 0 10px 10px;
  .conditions-container {
    flex-shrink: 0;
  }
  .result-container {
    .flex();
    flex-direction: column;
    flex: 1;
    .common-result-container-header {
      flex-shrink: 0;
    }
    .result-body {
      flex: 1;
      .flex();
      flex-direction: column;
      .table-container {
        flex: 1;
        .table-more-btns {
          color: var(--primary-color);
          margin-top: -20px;
          transform: translateY(-3px);
          margin-left: 20px;
          display: inline-block;
          cursor: pointer;
        }
        .status-text {
          background: #4bc964;
          border-radius: 4px;
          color: #fff;
          padding: 4px 15px;
          &.error {
            background: #ff3f4a;
          }
        }
      }
      .pager-container {
        flex-shrink: 0;
      }
    }
  }
}
</style>
